const rule = require("../rule");
const CardBox = require("../CardBox");
const Card = require("../Card");
const Player = require("../Player");
const CARDBOX = new CardBox();
const tester = new Player("test1");
// CARDBOX.moveCardsToPlayer(tester, 13);

tester.card_list.push(...
	"梅花_1,方块_2,梅花_3,方块_4,梅花_4,黑桃_4,黑桃_5,红心_5,方块_6,黑桃_7,方块_7,黑桃_8,黑桃_J".split(",").map(txt => Card.getIns(txt)));

// console.log("手牌为：");
// console.log(tester.card_list.sort_cards().toString());

console.assert(
	rule
	.check_straight(tester.card_list)
	.map(card_list => card_list.map(card => card.toString()).join("|"))
	.join("\n") ===
	['梅花_1|方块_2|梅花_3|方块_4|黑桃_5',
		'梅花_1|方块_2|梅花_3|梅花_4|黑桃_5',
		'梅花_1|方块_2|梅花_3|黑桃_4|黑桃_5',
		'梅花_1|方块_2|梅花_3|方块_4|红心_5',
		'梅花_1|方块_2|梅花_3|梅花_4|红心_5',
		'梅花_1|方块_2|梅花_3|黑桃_4|红心_5',
		'方块_2|梅花_3|方块_4|黑桃_5|方块_6',
		'方块_2|梅花_3|梅花_4|黑桃_5|方块_6',
		'方块_2|梅花_3|黑桃_4|黑桃_5|方块_6',
		'方块_2|梅花_3|方块_4|红心_5|方块_6',
		'方块_2|梅花_3|梅花_4|红心_5|方块_6',
		'方块_2|梅花_3|黑桃_4|红心_5|方块_6',
		'梅花_3|方块_4|黑桃_5|方块_6|黑桃_7',
		'梅花_3|梅花_4|黑桃_5|方块_6|黑桃_7',
		'梅花_3|黑桃_4|黑桃_5|方块_6|黑桃_7',
		'梅花_3|方块_4|红心_5|方块_6|黑桃_7',
		'梅花_3|梅花_4|红心_5|方块_6|黑桃_7',
		'梅花_3|黑桃_4|红心_5|方块_6|黑桃_7',
		'梅花_3|方块_4|黑桃_5|方块_6|方块_7',
		'梅花_3|梅花_4|黑桃_5|方块_6|方块_7',
		'梅花_3|黑桃_4|黑桃_5|方块_6|方块_7',
		'梅花_3|方块_4|红心_5|方块_6|方块_7',
		'梅花_3|梅花_4|红心_5|方块_6|方块_7',
		'梅花_3|黑桃_4|红心_5|方块_6|方块_7',
		'方块_4|黑桃_5|方块_6|黑桃_7|黑桃_8',
		'梅花_4|黑桃_5|方块_6|黑桃_7|黑桃_8',
		'黑桃_4|黑桃_5|方块_6|黑桃_7|黑桃_8',
		'方块_4|红心_5|方块_6|黑桃_7|黑桃_8',
		'梅花_4|红心_5|方块_6|黑桃_7|黑桃_8',
		'黑桃_4|红心_5|方块_6|黑桃_7|黑桃_8',
		'方块_4|黑桃_5|方块_6|方块_7|黑桃_8',
		'梅花_4|黑桃_5|方块_6|方块_7|黑桃_8',
		'黑桃_4|黑桃_5|方块_6|方块_7|黑桃_8',
		'方块_4|红心_5|方块_6|方块_7|黑桃_8',
		'梅花_4|红心_5|方块_6|方块_7|黑桃_8',
		'黑桃_4|红心_5|方块_6|方块_7|黑桃_8'
	]
	.join("\n"),
	"顺子检查算法"
);

tester.card_list.length = 0;
tester.card_list.push(...
	"梅花_1,黑桃_2,梅花_3,方块_4,梅花_4,黑桃_4,黑桃_5,红心_5,方块_6,黑桃_7,方块_7,黑桃_8,黑桃_J".split(",").map(txt => Card.getIns(txt)));

console.assert(
	rule.check_flush(tester.card_list)
	.map(card_list => card_list.map(card => card.toString()).join("|"))
	.join("\n") ===
	['黑桃_5|黑桃_J|黑桃_8|黑桃_7|黑桃_2',
		'黑桃_5|黑桃_J|黑桃_8|黑桃_7|黑桃_4',
		'黑桃_5|黑桃_J|黑桃_8|黑桃_2|黑桃_4',
		'黑桃_5|黑桃_J|黑桃_7|黑桃_2|黑桃_4',
		'黑桃_5|黑桃_8|黑桃_7|黑桃_2|黑桃_4',
		'黑桃_J|黑桃_8|黑桃_7|黑桃_2|黑桃_4'
	]
	.join("\n"),
	"同花检查算法"
);

tester.card_list.length = 0;
tester.card_list.push(...
	"梅花_1,梅花_2,梅花_3,方块_4,梅花_4,黑桃_4,黑桃_5,梅花_5,方块_6,黑桃_7,方块_7,黑桃_8,黑桃_J".split(",").map(txt => Card.getIns(txt)));

console.assert(
	rule.check_straight_flush(tester.card_list)
	.map(card_list => card_list.map(card => card.toString()).join("|"))
	.join("\n") ===
	['梅花_1|梅花_2|梅花_3|梅花_4|梅花_5']
	.join("\n"),
	"同花顺检查算法"
);


tester.card_list.length = 0;
tester.card_list.push(...
	"梅花_1,梅花_2,梅花_3,方块_4,梅花_4,黑桃_4,黑桃_5,梅花_7,方块_6,黑桃_7,方块_7,黑桃_8,方块_8".split(",").map(txt => Card.getIns(txt)));

console.assert(
	rule.check_three(tester.card_list)
	.map(card_list => card_list.map(card => card.toString()).join("|"))
	.join("\n") ===
	['方块_4|梅花_4|黑桃_4', '梅花_7|黑桃_7|方块_7']
	.join("\n"),
	"三条检查算法"
);

console.assert(
	rule.check_two(tester.card_list)
	.map(card_list => card_list.map(card => card.toString()).join("|"))
	.join("\n") ===
	['方块_4|梅花_4',
		'方块_4|黑桃_4',
		'梅花_4|黑桃_4',
		'梅花_7|黑桃_7',
		'梅花_7|方块_7',
		'黑桃_7|方块_7',
		'黑桃_8|方块_8'
	]
	.join("\n"),
	"两对检查算法"
);

tester.card_list.length = 0;
tester.card_list.push(...
	"红心_1,梅花_1,方块_1,黑桃_1,梅花_2,梅花_3,方块_4,梅花_4,黑桃_4,黑桃_5,方块_6,黑桃_8,方块_8".split(",").map(txt => Card.getIns(txt)));

console.assert(
	rule.check_full_house(tester.card_list)
	.map(card_list => card_list.map(card => card.toString()).join("|"))
	.join("\n") ===
	['红心_1|方块_1|黑桃_1|方块_4|梅花_4',
		'红心_1|方块_1|黑桃_1|方块_4|黑桃_4',
		'红心_1|方块_1|黑桃_1|梅花_4|黑桃_4',
		'红心_1|方块_1|黑桃_1|黑桃_8|方块_8',
		'红心_1|方块_1|梅花_1|方块_4|梅花_4',
		'红心_1|方块_1|梅花_1|方块_4|黑桃_4',
		'红心_1|方块_1|梅花_1|梅花_4|黑桃_4',
		'红心_1|方块_1|梅花_1|黑桃_8|方块_8',
		'红心_1|黑桃_1|梅花_1|方块_4|梅花_4',
		'红心_1|黑桃_1|梅花_1|方块_4|黑桃_4',
		'红心_1|黑桃_1|梅花_1|梅花_4|黑桃_4',
		'红心_1|黑桃_1|梅花_1|黑桃_8|方块_8',
		'方块_1|黑桃_1|梅花_1|方块_4|梅花_4',
		'方块_1|黑桃_1|梅花_1|方块_4|黑桃_4',
		'方块_1|黑桃_1|梅花_1|梅花_4|黑桃_4',
		'方块_1|黑桃_1|梅花_1|黑桃_8|方块_8',
		'方块_4|梅花_4|黑桃_4|红心_1|方块_1',
		'方块_4|梅花_4|黑桃_4|红心_1|黑桃_1',
		'方块_4|梅花_4|黑桃_4|红心_1|梅花_1',
		'方块_4|梅花_4|黑桃_4|方块_1|黑桃_1',
		'方块_4|梅花_4|黑桃_4|方块_1|梅花_1',
		'方块_4|梅花_4|黑桃_4|黑桃_1|梅花_1',
		'方块_4|梅花_4|黑桃_4|黑桃_8|方块_8'
	]
	.join("\n"),
	"葫芦检查算法"
);

tester.card_list.length = 0;
tester.card_list.push(...
	"梅花_1,梅花_2,梅花_3,梅花_4,黑桃_5".split(",").map(txt => Card.getIns(txt)));
console.assert(!rule.is_straight_flush(tester.card_list), "不是同花顺");

tester.card_list.length = 0;
tester.card_list.push(...
	"梅花_1,梅花_2,梅花_3,梅花_4,梅花_5".split(",").map(txt => Card.getIns(txt)));
console.assert(rule.is_straight_flush(tester.card_list), "是同花顺");

(() => { //同花顺
	let _pre_straight = 0;
	[
		"梅花_2,梅花_3,梅花_4,梅花_5,梅花_6",
		"梅花_3,梅花_4,梅花_5,梅花_6,梅花_7",
		"梅花_4,梅花_5,梅花_6,梅花_7,梅花_8",
		"梅花_5,梅花_6,梅花_7,梅花_8,梅花_9",
		"梅花_6,梅花_7,梅花_8,梅花_9,梅花_10",
		"梅花_7,梅花_8,梅花_9,梅花_10,梅花_J",
		"梅花_8,梅花_9,梅花_10,梅花_J,梅花_Q",
		"梅花_9,梅花_10,梅花_J,梅花_Q,梅花_K",
		"梅花_1,梅花_2,梅花_3,梅花_4,梅花_5",
		"梅花_10,梅花_J,梅花_Q,梅花_K,梅花_1",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_straight_flush(tester.card_list, res_info), card_names + "是同花顺");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //铁支
	let _pre_straight = 0;
	[
		"红心_2,梅花_2,方块_2,黑桃_2,梅花_1",
		"红心_3,梅花_3,方块_3,黑桃_3,梅花_2",
		"红心_3,梅花_3,方块_3,黑桃_3,梅花_4",
		"红心_3,梅花_3,方块_3,黑桃_3,梅花_1",
		"红心_4,梅花_4,方块_4,黑桃_4,梅花_1",
		"红心_K,梅花_K,方块_K,黑桃_K,梅花_5",
		"红心_1,梅花_1,方块_1,黑桃_1,梅花_5",
		"红心_1,梅花_1,方块_1,黑桃_1,梅花_6",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_four(tester.card_list, res_info), card_names + "是铁支");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //葫芦
	let _pre_straight = 0;
	[
		"红心_2,梅花_2,方块_2,黑桃_1,梅花_1",
		"红心_3,梅花_3,方块_3,黑桃_2,梅花_2",
		"红心_3,梅花_3,方块_3,黑桃_4,梅花_4",
		"红心_3,梅花_3,方块_3,黑桃_1,梅花_1",
		"红心_4,梅花_4,方块_4,黑桃_1,梅花_1",
		"红心_K,梅花_K,方块_K,黑桃_5,梅花_5",
		"红心_1,梅花_1,方块_1,黑桃_5,梅花_5",
		"红心_1,梅花_1,方块_1,黑桃_6,梅花_6",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_full_house(tester.card_list, res_info), card_names + "是葫芦");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //同花
	let _pre_straight = 0;
	[
		"红心_2,红心_3,红心_6,红心_7,红心_J",
		"梅花_2,梅花_3,梅花_6,梅花_7,梅花_Q",
		"梅花_2,梅花_3,梅花_6,梅花_8,梅花_Q",
		"梅花_1,梅花_3,梅花_6,梅花_8,梅花_2",
		"梅花_1,梅花_3,梅花_6,梅花_8,梅花_10",
		"黑桃_1,黑桃_3,黑桃_6,黑桃_8,黑桃_K",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_flush(tester.card_list, res_info), card_names + "是葫芦");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //顺子
	let _pre_straight = 0;
	[
		"梅花_2,梅花_3,梅花_4,梅花_5,梅花_6",
		"梅花_3,梅花_4,梅花_5,梅花_6,梅花_7",
		"梅花_4,梅花_5,梅花_6,梅花_7,梅花_8",
		"梅花_5,梅花_6,梅花_7,梅花_8,梅花_9",
		"梅花_6,梅花_7,梅花_8,梅花_9,梅花_10",
		"梅花_7,梅花_8,梅花_9,梅花_10,梅花_J",
		"梅花_8,梅花_9,梅花_10,梅花_J,梅花_Q",
		"梅花_9,梅花_10,梅花_J,梅花_Q,梅花_K",
		"梅花_1,梅花_2,梅花_3,梅花_4,梅花_5",
		"梅花_10,梅花_J,梅花_Q,梅花_K,梅花_1",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_straight(tester.card_list, res_info), card_names + "是顺子");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //三条
	let _pre_straight = 0;
	[
		"红心_2,梅花_2,方块_2,黑桃_1,梅花_J",
		"红心_3,梅花_3,方块_3,黑桃_2,梅花_J",
		"红心_3,梅花_3,方块_3,黑桃_4,梅花_J",
		"红心_3,梅花_3,方块_3,黑桃_1,梅花_J",
		"红心_4,梅花_4,方块_4,黑桃_1,梅花_J",
		"红心_K,梅花_K,方块_K,黑桃_5,梅花_J",
		"红心_1,梅花_1,方块_1,黑桃_5,梅花_J",
		"红心_1,梅花_1,方块_1,黑桃_6,梅花_J",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_three(tester.card_list, res_info), card_names + "是三条");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //两对
	let _pre_straight = 0;
	[
		"红心_3,梅花_3,方块_2,黑桃_2,梅花_J",
		"红心_3,梅花_3,方块_2,黑桃_2,梅花_K",
		"红心_3,梅花_3,方块_4,黑桃_4,梅花_J",
		"红心_3,梅花_3,方块_4,黑桃_4,梅花_1",
		"红心_2,梅花_2,方块_1,黑桃_1,梅花_J",
		"红心_3,梅花_3,方块_1,黑桃_1,梅花_J",
		"红心_3,梅花_3,方块_1,黑桃_1,梅花_K",
		"红心_4,梅花_4,方块_1,黑桃_1,梅花_2",
		"红心_K,梅花_K,方块_1,黑桃_1,梅花_J",
		"红心_1,梅花_1,红心_K,梅花_K,梅花_Q",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_two_pair(tester.card_list, res_info), card_names + "是两对");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //对子
	let _pre_straight = 0;
	[
		"红心_2,梅花_2,方块_9,黑桃_1,梅花_J",
		"红心_3,梅花_3,方块_9,黑桃_2,梅花_J",
		"红心_3,梅花_3,方块_9,黑桃_4,梅花_J",
		"红心_3,梅花_3,方块_9,黑桃_2,梅花_K",
		"红心_3,梅花_3,方块_9,黑桃_4,梅花_1",
		"红心_3,梅花_3,方块_9,黑桃_1,梅花_J",
		"红心_3,梅花_3,方块_9,黑桃_1,梅花_K",
		"红心_4,梅花_4,方块_9,黑桃_1,梅花_2",
		"红心_K,梅花_K,方块_9,黑桃_1,梅花_J",
		"红心_1,梅花_1,红心_9,梅花_K,梅花_Q",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_two(tester.card_list, res_info), card_names + "是对子");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();

(() => { //散牌
	let _pre_straight = 0;
	[
		"红心_3,梅花_8,方块_9,黑桃_2,梅花_J",
		"红心_3,梅花_8,方块_9,黑桃_4,梅花_J",
		"红心_3,梅花_8,方块_9,黑桃_2,梅花_K",
		"红心_4,梅花_8,方块_9,黑桃_1,梅花_2",
		"红心_3,梅花_8,方块_9,黑桃_4,梅花_1",
		"红心_2,梅花_8,方块_9,黑桃_1,梅花_J",
		"红心_3,梅花_8,方块_9,黑桃_1,梅花_J",
		"红心_3,梅花_8,方块_9,黑桃_1,梅花_K",
		"红心_K,梅花_8,方块_9,黑桃_1,梅花_J",
		"红心_1,梅花_8,红心_9,梅花_K,梅花_Q",
	].forEach(card_names => {
		const res_info = {}
		tester.card_list.length = 0;
		tester.card_list.push(...card_names.split(",").map(txt => Card.getIns(txt)));
		console.assert(rule.is_any(tester.card_list, res_info), card_names + "是散牌");
		console.assert(res_info.weights_value > _pre_straight, `${card_names}权重正确(${res_info.weights_value},${_pre_straight})`);
		_pre_straight = res_info.weights_value;
	});
})();



console.log("rule.js all pass!");